package com.shanzmoo.admin.web.sys;

import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.sys.entity.dto.MenuSaveDto;
import com.shanzmoo.db.sys.entity.dto.MenuUpdateDto;
import com.shanzmoo.db.sys.entity.vo.MenuAuthVo;
import com.shanzmoo.db.sys.entity.vo.MenuTreeVo;
import com.shanzmoo.db.sys.entity.vo.MenuVo;
import com.shanzmoo.db.sys.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 系统菜单表 前端控制器
 * </p>
 *
 * @author Gzhao
 * @since 2020-11-10
 */
@Api(tags = "系统-菜单管理")
@RestController
@RequestMapping("/api/sys/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody MenuSaveDto saveDto) {
        SResponse<Boolean> re = menuService.add(saveDto, CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "menuId") Integer  menuId) {
        SResponse<Boolean> re = menuService.deleteById(menuId, CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody MenuUpdateDto updateDto) {
        SResponse<Boolean> re = menuService.editMenu(updateDto, CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "简要菜单树（用于角色权限管理处）")
    @GetMapping("/briefTree")
    public SHttpResponse<List<MenuTreeVo>> getBriefTree() {
        SResponse<List<MenuTreeVo>> re = menuService.getBriefTree();
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "菜单列表树")
    @GetMapping("/allTree")
    public SHttpResponse<List<MenuVo>> getAllTree() {
        SResponse<List<MenuVo>> re = menuService.getAllTree();
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "人员菜单")
    @GetMapping("/authTree")
    public SHttpResponse<List<MenuAuthVo>> getAuthTree() {
        SResponse<List<MenuAuthVo>> re = menuService.getAuthTree(CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }

}
